package com.baosight.sczx.zm.ck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ck.domain.ZMCKTCCR;

/**
 * 
 * <p>Title: ServiceZMCK00.java</p>
 * <p>Description: 停产撤人方案</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月12日 下午2:11:13  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCK00 extends ServiceEPBase {
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始进入方法</p>
	 * <p>return value: ServiceZMCK00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日下午2:11:24 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法 </p>
	 * <p>return value: ServiceZMCK00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日下午2:11:33 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");// 矿ID的默认值
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCKTCCR.query", new ZMCKTCCR());

		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ck.status", new String[] { "草拟", "上报", "关闭" }, new String[] { "草拟", "上报", "关闭" }, false));// 状态
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", fdMiningId, false));// 班次
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));

		outInfo.set("fdCreatorId", UserSession.getUserId());// 登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));// 单位的默认值
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));// 单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));// 矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));// 矿名称的默认值

		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description:  新增方法</p>
	 * <p>return value: ServiceZMCK00</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日下午2:11:46 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCKTCCR _dom = new ZMCKTCCR();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_TCCR"));
			_dom.setFdStatus("草拟");
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString()); // 矿井id
			this.dao.insert("ZMCKTCCR.insert", _dom);

		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK00</p>
	 * <p>throws Exception:修改方法</p>
	 * <p>History:
	 *    2016年9月12日下午2:12:00 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCKTCCR _dom = new ZMCKTCCR();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			boolean isHasDetail = this.isHasDetail(_dom.getFdKeyId());
			if (!isHasDetail) {
				out = this.query(inInfo);
				out.setStatus(-1);
				String msg = "该记录下有反馈信息存在,不能修改该记录！";
				out.setMsg(msg);
				return out;
			}
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString()); // 矿井id
			this.dao.update("ZMCKTCCR.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}
	
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK00</p>
	 * <p>throws Exception:删除方法</p>
	 * <p>History:
	 *    2016年9月12日下午2:12:14 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo outInfo = null;
		List<String> _list = new ArrayList<String>();
		Map<String, List<String>> params = new HashMap<String, List<String>>();

		for (int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++) {
			String fdTccrId = inInfo.getCellStr("result", i, "fdKeyId");
			boolean isHasDetail = this.isHasDetail(fdTccrId);
			if (!isHasDetail) {
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				String msg = "该记录下有反馈信息存在,不能删除该记录！";
				outInfo.setMsg(msg);
				return outInfo;
			}

			EiInfo _temp = super.delete(inInfo, "ZMCKTCCR.delete");
			outInfo = this.query(inInfo);
			outInfo.setMsg(_temp.getMsg());
			outInfo.setDetailMsg(_temp.getDetailMsg());
		}

		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: report</p>
	 * <p>Description: 上报方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日下午2:12:25 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCKTCCR.report");

		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());

		return out;
	}
	
	/**
	 * 
	 * <p>Function: close</p>
	 * <p>Description: 关闭方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日下午2:12:39 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo close(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCKTCCR _dom = new ZMCKTCCR();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			// 判断是否有明细
			boolean validate = this.isHasDetail(_dom.getFdKeyId());
			if (validate) {
				inInfo.setStatus(-1);
				inInfo = this.query(inInfo);
				inInfo.setMsg("该停产撤人方案中没有反馈信息，请添加后关闭！");
				return inInfo;
			}

			this.dao.update("ZMCKTCCR.close", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}
	
	/**
	 * 
	 * <p>Function: isHasDetail</p>
	 * <p>Description: 判断主表记录可否删除、修改(当下面有反馈信息时，不可删除/修改）</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月12日上午9:50:07 郭淑芳
	 * </p>
	 *
	 */
	private boolean isHasDetail(String fdTccrId) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fdTccrId", fdTccrId);
		List result = dao.query("ZMCKTCCRReport.query", params);
		if (result == null || result.isEmpty()) {
			return true;
		}
		return false;
	}
}
